home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / VISCAFE.BIN / ComboBox.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-19  |  9.7 KB  |  562 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Font;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.LayoutManager;
  12. import java.awt.Panel;
  13. import java.awt.TextField;
  14. import symantec.itools.lang.OS;
  15.  
  16. public class ComboBox extends Panel {
  17.    public static final int EVT_IMAGE_SELECT = 16388;
  18.    private boolean bEditable;
  19.    private boolean bSearchable;
  20.    private boolean bCaseSensitive;
  21.    private TextField editBox;
  22.    private DirectionButton arrow;
  23.    private ImageListBox list;
  24.    private boolean bDown;
  25.    // $FF: renamed from: ix int
  26.    private int field_0;
  27.    // $FF: renamed from: iy int
  28.    private int field_1;
  29.    private int iwidth;
  30.    private int iheight;
  31.    private int lheight;
  32.    private boolean bOverList;
  33.    private boolean showHScroll;
  34.    private boolean showVScroll;
  35.    private Font editFont;
  36.    private Font dropFont;
  37.    private static boolean bOsFlag;
  38.    private static ComboBox currentlyDown = null;
  39.    private Component parent;
  40.    private String sSearchText;
  41.    private String sSearchItem;
  42.    private String sLastText;
  43.    private String sSearchOrig;
  44.    private int searchIndex;
  45.    private int searchLen;
  46.    private int searchI;
  47.  
  48.    public ComboBox() {
  49.       this(false, false);
  50.    }
  51.  
  52.    public ComboBox(boolean var1, boolean var2) {
  53.       this.bDown = false;
  54.       this.bOverList = false;
  55.       this.showHScroll = false;
  56.       this.showVScroll = true;
  57.       bOsFlag = !System.getProperty("os.name").startsWith("Win");
  58.       ((Container)this).setLayout((LayoutManager)null);
  59.       this.editBox = new TextField("");
  60.       this.editBox.setBackground(Color.white);
  61.       this.arrow = new DirectionButton(3);
  62.       this.list = new ImageListBox("ILB");
  63.       this.list.setComboMode(true);
  64.       this.list.setShowHorizontalScroll(false);
  65.       ((Container)this).add(this.list);
  66.       ((Container)this).add(this.editBox);
  67.       ((Container)this).add(this.arrow);
  68.       this.arrow.setShowFocus(false);
  69.       this.arrow.shrinkTriangle(1, 1, 5, 5);
  70.       this.arrow.disable();
  71.       this.lheight = 0;
  72.       this.list.hide();
  73.       this.setEditable(var1);
  74.       this.setSearchable(var2);
  75.       this.bCaseSensitive = true;
  76.    }
  77.  
  78.    public void setEditable(boolean var1) {
  79.       this.bEditable = var1;
  80.       this.editBox.setEditable(this.bEditable || this.bSearchable);
  81.    }
  82.  
  83.    public boolean getEditable() {
  84.       return this.bEditable;
  85.    }
  86.  
  87.    public void setSearchable(boolean var1) {
  88.       this.bSearchable = var1;
  89.       this.editBox.setEditable(this.bEditable || this.bSearchable);
  90.    }
  91.  
  92.    public boolean getSearchable() {
  93.       return this.bSearchable;
  94.    }
  95.  
  96.    public void setCaseSensitive(boolean var1) {
  97.       this.bCaseSensitive = var1;
  98.    }
  99.  
  100.    public boolean getCaseSensitive() {
  101.       return this.bCaseSensitive;
  102.    }
  103.  
  104.    public void setShowHorizontalScroll(boolean var1) {
  105.       this.showHScroll = var1;
  106.       this.list.setShowHorizontalScroll(var1);
  107.    }
  108.  
  109.    public boolean getShowHorizontalScroll() {
  110.       return this.showHScroll;
  111.    }
  112.  
  113.    public void setShowVerticalScroll(boolean var1) {
  114.       this.showVScroll = var1;
  115.       this.list.setShowVerticalScroll(var1);
  116.    }
  117.  
  118.    public boolean getShowVerticalScroll() {
  119.       return this.showVScroll;
  120.    }
  121.  
  122.    public synchronized void setComboBoxFont(Font var1) {
  123.       this.setFont(var1);
  124.    }
  125.  
  126.    public synchronized Font getComboBoxFont() {
  127.       return this.editFont;
  128.    }
  129.  
  130.    public synchronized void setEditFieldFont(Font var1) {
  131.       if (this.editBox != null && var1 != null && this.iheight > 15) {
  132.          this.editFont = var1;
  133.          this.editBox.setFont(var1);
  134.       }
  135.  
  136.    }
  137.  
  138.    public synchronized Font getEditFieldFont() {
  139.       return this.editFont;
  140.    }
  141.  
  142.    public synchronized void setDropDownFont(Font var1) {
  143.       if (this.list != null && var1 != null) {
  144.          this.dropFont = var1;
  145.          this.list.setFont(var1);
  146.       }
  147.  
  148.    }
  149.  
  150.    public synchronized Font getDropDownFont() {
  151.       return this.dropFont;
  152.    }
  153.  
  154.    public boolean handleEvent(Event var1) {
  155.       switch (var1.id) {
  156.          case 401:
  157.             this.sLastText = this.editBox.getText();
  158.             break;
  159.          case 402:
  160.             if (var1.target == this.editBox) {
  161.                this.verify();
  162.             }
  163.             break;
  164.          case 404:
  165.             if (var1.target != this.editBox) {
  166.                switch (var1.key) {
  167.                   case 1000:
  168.                      this.list.select(0);
  169.                      break;
  170.                   case 1001:
  171.                      this.list.select(this.list.countItems() - 1);
  172.                      break;
  173.                   case 1002:
  174.                   case 1004:
  175.                      this.list.select(this.list.getSelectedIndex() - 1);
  176.                      break;
  177.                   case 1003:
  178.                   case 1005:
  179.                      this.list.select(this.list.getSelectedIndex() + 1);
  180.                }
  181.  
  182.                this.editBox.setText(this.list.getSelectedItem());
  183.                this.editBox.selectAll();
  184.             }
  185.             break;
  186.          case 504:
  187.             if (var1.target == this.list) {
  188.                this.bOverList = true;
  189.             }
  190.             break;
  191.          case 505:
  192.             if (var1.target == this.list) {
  193.                this.bOverList = false;
  194.             }
  195.             break;
  196.          case 701:
  197.             this.editBox.setText(this.list.getSelectedItem());
  198.             if (this.parent == null) {
  199.                this.parent = ((Component)this).getParent();
  200.             }
  201.  
  202.             if (this.parent != null) {
  203.                var1.target = this;
  204.                var1.id = 1001;
  205.                this.parent.postEvent(var1);
  206.             }
  207.  
  208.             this.dropList(false);
  209.             return true;
  210.          case 1001:
  211.             if (var1.target instanceof DirectionButton) {
  212.                this.dropList(!this.bDown);
  213.                return true;
  214.             }
  215.  
  216.             if (var1.target == this.list) {
  217.                return true;
  218.             }
  219.             break;
  220.          case 1004:
  221.             if (var1.target == this && !this.bDown) {
  222.                this.editBox.selectAll();
  223.                this.editBox.requestFocus();
  224.             } else if (bOsFlag && var1.target == this.editBox) {
  225.                this.dropList(false);
  226.             }
  227.             break;
  228.          case 1005:
  229.             if (var1.target == this.arrow && !this.bOverList) {
  230.                this.dropList(false);
  231.             }
  232.             break;
  233.          case 16388:
  234.             if (this.parent == null) {
  235.                this.parent = ((Component)this).getParent();
  236.             }
  237.  
  238.             if (this.parent != null) {
  239.                var1.target = this;
  240.                this.parent.postEvent(var1);
  241.             }
  242.  
  243.             return true;
  244.       }
  245.  
  246.       return super.handleEvent(var1);
  247.    }
  248.  
  249.    public static boolean addFirstTopDown() {
  250.       return !addLastBottomUp();
  251.    }
  252.  
  253.    public static boolean addLastBottomUp() {
  254.       String var0 = System.getProperty("os.name");
  255.       if (!var0.startsWith("S") && !var0.startsWith("Kona") && !var0.startsWith("AIX") && !var0.startsWith("OSF")) {
  256.          return var0.startsWith("Win") && System.getProperty("java.vendor").startsWith("Netscape") && System.getProperty("java.version").equals("1.02");
  257.       } else {
  258.          return true;
  259.       }
  260.    }
  261.  
  262.    public static boolean needsPlatformHelp() {
  263.       return bOsFlag;
  264.    }
  265.  
  266.    public static void unDropLastDropped() {
  267.       if (currentlyDown != null) {
  268.          currentlyDown.dropList(false);
  269.       }
  270.  
  271.    }
  272.  
  273.    public void clear() {
  274.       this.list.clear();
  275.       this.editBox.setText("");
  276.    }
  277.  
  278.    public synchronized void addItem(String var1) {
  279.       this.addItem((Image)null, var1, true);
  280.    }
  281.  
  282.    public synchronized void addItem(String var1, boolean var2) {
  283.       this.addItem((Image)null, var1, var2);
  284.    }
  285.  
  286.    public synchronized void addItem(Image var1, String var2) {
  287.       this.addItem(var1, var2, true);
  288.    }
  289.  
  290.    public synchronized void addItem(Image var1, String var2, boolean var3) {
  291.       this.list.addItem(var1, var2, var3);
  292.       this.arrow.enable();
  293.    }
  294.  
  295.    public void setListItems(String[] var1) {
  296.       this.clear();
  297.  
  298.       for(int var2 = 0; var2 < var1.length; ++var2) {
  299.          this.addItem(var1[var2]);
  300.       }
  301.  
  302.    }
  303.  
  304.    public String[] getListItems() {
  305.       int var1 = this.countItems();
  306.       String[] var2 = new String[var1];
  307.  
  308.       for(int var3 = 0; var3 < var1; ++var3) {
  309.          var2[var3] = this.getItem(var3);
  310.       }
  311.  
  312.       return var2;
  313.    }
  314.  
  315.    public int countItems() {
  316.       return this.list.countItems();
  317.    }
  318.  
  319.    public String getItem(int var1) {
  320.       return this.list.getItem(var1);
  321.    }
  322.  
  323.    public void changeImage(int var1, Image var2) {
  324.       this.list.changeImage(var1, var2);
  325.    }
  326.  
  327.    public void changeText(int var1, String var2) {
  328.       this.list.changeText(var1, var2);
  329.    }
  330.  
  331.    public void enable(int var1) {
  332.       this.list.enable(var1);
  333.    }
  334.  
  335.    public void disable(int var1) {
  336.       this.list.disable(var1);
  337.    }
  338.  
  339.    public void enable(int var1, boolean var2) {
  340.       if (var2) {
  341.          this.list.enable(var1);
  342.       } else {
  343.          this.list.disable(var1);
  344.       }
  345.    }
  346.  
  347.    public synchronized void delItem(int var1) {
  348.       this.delItems(var1, var1);
  349.    }
  350.  
  351.    public synchronized void delItems(int var1, int var2) {
  352.       this.list.delItems(var1, var2);
  353.       if (this.list.countItems() == 0) {
  354.          this.arrow.disable();
  355.          this.dropList(false);
  356.       }
  357.  
  358.    }
  359.  
  360.    public synchronized void delSelectedItem() {
  361.       this.list.delSelectedItems();
  362.    }
  363.  
  364.    public String getText() {
  365.       this.verify();
  366.       return this.editBox.getText();
  367.    }
  368.  
  369.    public String getSelectedItem() {
  370.       return this.list.getSelectedItem();
  371.    }
  372.  
  373.    public int getSelectedIndex() {
  374.       return this.list.getSelectedIndex();
  375.    }
  376.  
  377.    public synchronized void select(int var1) {
  378.       if (var1 >= 0 && var1 <= this.list.countItems()) {
  379.          this.list.select(var1);
  380.          this.editBox.setText(this.list.getSelectedItem());
  381.          if (bOsFlag) {
  382.             ((Component)this).repaint();
  383.          }
  384.       }
  385.  
  386.    }
  387.  
  388.    public void select(String var1) {
  389.       this.list.select(var1);
  390.       this.editBox.setText(this.list.getSelectedItem());
  391.       if (bOsFlag) {
  392.          ((Component)this).repaint();
  393.       }
  394.  
  395.    }
  396.  
  397.    public synchronized void deselect(int var1) {
  398.       this.list.deselect(var1);
  399.       this.editBox.setText("");
  400.    }
  401.  
  402.    public synchronized boolean isSelected(int var1) {
  403.       return this.list.isSelected(var1);
  404.    }
  405.  
  406.    public synchronized void setFont(Font var1) {
  407.       this.editFont = this.dropFont = var1;
  408.       this.setEditFieldFont(var1);
  409.       this.setDropDownFont(var1);
  410.       super.setFont(var1);
  411.    }
  412.  
  413.    public void dropList(boolean var1) {
  414.       if (this.bDown != var1) {
  415.          this.bDown = var1;
  416.          if (var1) {
  417.             if (currentlyDown != null && currentlyDown != this) {
  418.                currentlyDown.dropList(false);
  419.             }
  420.  
  421.             currentlyDown = this;
  422.             this.list.show();
  423.             this.list.setDirty();
  424.             this.list.repaint();
  425.             if (((Component)this).getParent() != null) {
  426.                this.lheight = Math.min(((Component)this).getParent().bounds().height - this.field_1 - this.iheight - 10, this.list.minimumSize().height + 3);
  427.             } else {
  428.                this.lheight = this.list.minimumSize().height + 3;
  429.             }
  430.  
  431.             this.arrow.setDirection(2);
  432.          } else {
  433.             this.lheight = 0;
  434.             currentlyDown = null;
  435.             this.list.hide();
  436.             this.arrow.setDirection(3);
  437.             ((Component)this).invalidate();
  438.          }
  439.  
  440.          this.arrow.repaint();
  441.          super.reshape(this.field_0, this.field_1, this.iwidth, this.iheight + this.lheight);
  442.       }
  443.    }
  444.  
  445.    public synchronized void reshape(int var1, int var2, int var3, int var4) {
  446.       this.field_0 = var1;
  447.       this.field_1 = var2;
  448.       this.iwidth = var3;
  449.       if (bOsFlag) {
  450.          this.iheight = Math.max(29, var4);
  451.       } else {
  452.          this.iheight = var4;
  453.       }
  454.  
  455.       if (var4 > 22) {
  456.          var4 -= 22;
  457.          if (bOsFlag) {
  458.             var2 = 7;
  459.             var1 = 7;
  460.          } else {
  461.             var2 = 5;
  462.             var1 = 5;
  463.          }
  464.  
  465.          while(var4 > 0) {
  466.             ++var1;
  467.             --var4;
  468.             if (var4 > 0) {
  469.                ++var2;
  470.                --var4;
  471.             }
  472.          }
  473.  
  474.          this.arrow.shrinkTriangle(1, 1, var1, var2);
  475.       } else {
  476.          this.arrow.shrinkTriangle(1, 1, 5, 5);
  477.       }
  478.  
  479.       super.reshape(this.field_0, this.field_1, this.iwidth, this.iheight + this.lheight);
  480.    }
  481.  
  482.    public void paint(Graphics var1) {
  483.       super.paint(var1);
  484.       Dimension var2 = ((Component)this).size();
  485.       if (this.bDown) {
  486.          this.list.reshape(0, this.iheight + 1, var2.width, this.lheight);
  487.       }
  488.  
  489.       if (OS.isMacintosh()) {
  490.          this.editBox.reshape(0, (int)((double)this.iheight * 0.1), var2.width - 16, (int)((double)this.iheight * 0.8));
  491.       } else {
  492.          this.editBox.reshape(0, 0, var2.width - 16, this.iheight);
  493.       }
  494.  
  495.       this.editBox.setText(this.list.getSelectedItem());
  496.       if (OS.isMacintosh()) {
  497.          this.arrow.reshape(var2.width - 16, (int)((double)this.iheight * 0.1), 15, (int)((double)this.iheight * 0.8));
  498.       } else if (bOsFlag) {
  499.          this.arrow.reshape(var2.width - 16, 2, 15, this.iheight - 4);
  500.       } else {
  501.          this.arrow.reshape(var2.width - 16, 1, 15, this.iheight - 2);
  502.       }
  503.    }
  504.  
  505.    public Dimension preferredSize() {
  506.       Dimension var1 = ((Component)this).size();
  507.       Dimension var2 = this.minimumSize();
  508.       return new Dimension(Math.max(var1.width, var2.width), Math.max(var1.height, var2.height));
  509.    }
  510.  
  511.    public Dimension minimumSize() {
  512.       Dimension var1 = this.editBox.minimumSize();
  513.       var1.width += 17;
  514.       if (var1.height == 0) {
  515.          if (bOsFlag) {
  516.             var1.height = 29;
  517.          } else {
  518.             var1.height = 21;
  519.          }
  520.       }
  521.  
  522.       return var1;
  523.    }
  524.  
  525.    private void verify() {
  526.       if (this.bSearchable) {
  527.          this.searchIndex = this.editBox.getSelectionStart();
  528.          this.sSearchOrig = this.editBox.getText().substring(0, this.searchIndex);
  529.          if (this.bCaseSensitive) {
  530.             this.sSearchText = this.sSearchOrig;
  531.          } else {
  532.             this.sSearchText = this.sSearchOrig.toUpperCase();
  533.          }
  534.  
  535.          this.searchLen = this.list.countItems();
  536.          if (this.sSearchText.length() > 0) {
  537.             for(this.searchI = 0; this.searchI < this.searchLen; ++this.searchI) {
  538.                if (this.bCaseSensitive) {
  539.                   this.sSearchItem = this.list.getItem(this.searchI);
  540.                } else {
  541.                   this.sSearchItem = this.list.getItem(this.searchI).toUpperCase();
  542.                }
  543.  
  544.                if (this.sSearchItem.startsWith(this.sSearchText)) {
  545.                   this.editBox.setText(this.sSearchOrig);
  546.                   this.list.select(this.searchI);
  547.                   this.editBox.select(this.searchIndex, this.searchIndex);
  548.                   break;
  549.                }
  550.             }
  551.  
  552.             if (!this.bEditable && this.searchI == this.searchLen) {
  553.                this.editBox.setText(this.sLastText);
  554.                this.editBox.select(this.searchIndex - 1, this.searchIndex - 1);
  555.             }
  556.          }
  557.       }
  558.  
  559.       this.sLastText = this.editBox.getText();
  560.    }
  561. }
  562.